package com.ln.lnzw.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ActivityUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ln.lnzw.R;
import com.ln.lnzw.activity.AnnouncementMoreActivity;
import com.ln.lnzw.activity.HandleaffairsServiceActivity;
import com.ln.lnzw.activity.UserAgreementActivity;
import com.ln.lnzw.adapter.AnnouncementAdapter;
import com.ln.lnzw.app.AppConstant;
import com.ln.lnzw.base.BaseFragment;
import com.ln.lnzw.bean.WcmBean;
import com.ln.lnzw.net.HttpMethods;
import com.ln.lnzw.utils.ImageUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wuwei on 2018/4/2.
 * 通知公告
 */

public class AnnouncementFragment extends BaseFragment {

    @BindView(R.id.rv_recyclerview)
    RecyclerView rvRecyclerview;
    Unbinder unbinder;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.nestedScrollView)
    NestedScrollView nestedScrollView;
    @BindView(R.id.ll_jazaigengduo)
    LinearLayout llJazaigengduo;
    @BindView(R.id.iv_quanzeqingdan)
    ImageView ivQuanzeqingdan;
    @BindView(R.id.iv_shoufeiqingdan)
    ImageView ivShoufeiqingdan;
    @BindView(R.id.iv_zhengwuqingdan)
    ImageView ivZhengwuqingdan;
    @BindView(R.id.iv_muluqingdan)
    ImageView ivMuluqingdan;
    @BindView(R.id.iv_shixiangqingdan)
    ImageView ivShixiangqingdan;
    private AnnouncementAdapter mAdapter;

    private List<WcmBean.ListDatasBean> mList = new ArrayList<>();
    private List<WcmBean.ListDatasBean> sList = new ArrayList<>();

    private String url;
    private String moreUrl;
    private String name;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.announcementfragment_activity, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setView();
        initData();
        serviceList();
    }

    private void serviceList() {
        HttpMethods.getInstance().info.getWcmData(AppConstant.SERVICE_LIST_URL)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<WcmBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }
                    @Override
                    public void onNext(WcmBean wcmBean) {
                        if (sList != null) {
                            sList.clear();
                        }
                        sList.addAll(wcmBean.getList_datas());
                    }
                    @Override
                    public void onError(Throwable e) {

                    }
                    @Override
                    public void onComplete() {
                        nestedScrollView.setVisibility(View.VISIBLE);
                    }
                });

    }

    private void setView() {
        ImageUtil.suitDisplay2(ivQuanzeqingdan, 510, 225);
        ImageUtil.suitDisplay2(ivShoufeiqingdan, 510, 225);
        ImageUtil.suitDisplay2(ivMuluqingdan, 510, 225);
        ImageUtil.suitDisplay2(ivShixiangqingdan, 510, 225);
        ImageUtil.suitDisplay1(ivZhengwuqingdan, 211, 39);


        url = getArguments().getString("url");
        moreUrl = getArguments().getString("moreUrl");
        name = getArguments().getString("name");
        rvRecyclerview.setNestedScrollingEnabled(false);
        rvRecyclerview.setLayoutManager(new LinearLayoutManager(activity));
        mAdapter = new AnnouncementAdapter(R.layout.item_announcement, mList);
        rvRecyclerview.addOnItemTouchListener(new OnItemClickListener() {

            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle b = new Bundle();
                b.putString("webUrl",mList.get(position).getUrl());
               ActivityUtils.startActivity(b,activity,UserAgreementActivity.class);
            }
        });

        rvRecyclerview.setAdapter(mAdapter);
        //屏蔽上拉加载
        refreshLayout.setEnableLoadMore(false);
        //设置下拉刷新监听事件
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        //结束加载
                        refreshLayout.finishRefresh();
//                        //加载失败的话3秒后结束加载
//                        refreshLayout.finishRefresh(3000);
                    }
                }, 3000);
            }
        });


    }

    private void initData() {
        HttpMethods.getInstance().info.getWcmData(url)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<WcmBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(WcmBean wcmBean) {
                        if (mList != null) {
                            mList.clear();
                        }
                        mList.addAll(wcmBean.getList_datas());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.ll_jazaigengduo)
    public void onViewClicked() {
        Bundle b = new Bundle();
        b.putString("moreUrl", moreUrl);
        b.putString("name", name);
        ActivityUtils.startActivity(b, activity, AnnouncementMoreActivity.class);

    }

    @OnClick({R.id.iv_quanzeqingdan, R.id.iv_shoufeiqingdan, R.id.iv_zhengwuqingdan, R.id.iv_muluqingdan, R.id.iv_shixiangqingdan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_quanzeqingdan://权责清单
                listSelection("权责清单");
                break;
            case R.id.iv_shoufeiqingdan://收费清单
                listSelection("收费清单");
                break;
            case R.id.iv_zhengwuqingdan://政务服务清单
                listSelection("政务服务事项目录");
                break;
            case R.id.iv_muluqingdan://目录清单
                listSelection("目录清单");
                break;
            case R.id.iv_shixiangqingdan://事项清单
                listSelection("事项清单");
                break;
        }
    }

    private void listSelection(String name) {
        for (int i=0;i<sList.size();i++){
             if (name.equals(sList.get(i).getCname())){
                 //带着sList.get(i).getCurl()传到下一页
                  Bundle b = new Bundle();
                 b.putString("webUrl",sList.get(i).getCurl());
                 b.putString("name",sList.get(i).getCname());
                 ActivityUtils.startActivity(b,activity, UserAgreementActivity.class);
             }
        }
    }
}
